package com.gupaoedu.vip.pattern.observer.subject;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/6.
 */
public class EventLisenter {

    //注册时间容器
    protected Map<Enum,Event> map =new HashMap<>();

    public void addLisenter(Enum eventType, Object target, Method callback){
        map.put(eventType,new Event(target,callback));
    }

    private void trigger(Event event){
        event.setSource(this);
        event.setTime(System.currentTimeMillis());
        try {
            event.getCallback().invoke(event.getTarget(),event);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void trigger(Enum type){
        if(!map.containsKey(type))return;
        trigger(this.map.get(type).setTrigger(type.toString()));
    }
}